package com.lvyq.myplayer.database

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import com.lvyq.myplayer.dao.CurrentPlayLstDao
import com.lvyq.myplayer.dao.PlayCacheDao
import com.lvyq.myplayer.dao.PlayCollectDao
import com.lvyq.myplayer.dao.PlayHistoryDao
import com.lvyq.myplayer.model.cache.PlayCacheEntity
import com.lvyq.myplayer.model.collect.PlayCollectEntity
import com.lvyq.myplayer.model.current.CurrentPlayLstEntity
import com.lvyq.myplayer.model.history.PlayHistoryEntity
import com.lvyq.myplayer.parcelable.SongInfoData

/**
 * 文件描述：PlayHistoryDatabase
 * 作者：lvyq
 * 创建时间：2025-06-16
 * 修改时间：2025-06-16
 */

@Database(
    //这里播放记录和收藏用同一个库。
    entities = [PlayHistoryEntity::class,
           PlayCollectEntity::class,
           PlayCacheEntity::class,
           CurrentPlayLstEntity::class],
    version = 1,
    exportSchema = false)
abstract class PlayerDatabase : RoomDatabase() {
      abstract fun playHistoryDao(): PlayHistoryDao
    abstract fun playCollectDao(): PlayCollectDao
    abstract fun playCacheDao(): PlayCacheDao

    abstract fun  currentPlayLstDao(): CurrentPlayLstDao
      companion object {
        @Volatile
         private var INSTANCE: PlayerDatabase? = null

        fun getDatabase(context: Context): PlayerDatabase {
              return INSTANCE ?: synchronized(this) {
                val instance = Room.databaseBuilder(
                 context.applicationContext,
                    PlayerDatabase::class.java,
                    "lvyq_xs_player"
               )
                    .build()
                    INSTANCE = instance
                    instance
                 }
             }

          //fallbackToDestructiveMigration() 升级清零数据
          //.addMigrations(MIGRATION_1_2) // 添加升级迁移逻辑
          // 添加迁移策略 防止数据晴空
          private val MIGRATION_1_2 = object : Migration(1, 2) {
              override fun migrate(database: SupportSQLiteDatabase) {

              }
          }
         }

}
